/**
 *
 */
angular
    .module('baocloud.ChartView')
    .controller('ChartViewController', ChartViewController)
    .controller('sortableController', sortableController)
    .controller('connectedController', connectedController);

function ChartViewController($scope, $timeout) {
    var self = this;
    var builderJson = {
        "all": 10887,
        "charts": {
            "map": 3237,
            "lines": 2164,
            "bar": 7561,
            "line": 7778,
            "pie": 7355,
            "scatter": 2405,
            "candlestick": 1842,
            "radar": 2090,
            "heatmap": 1762,
            "treemap": 1593,
            "graph": 2060,
            "boxplot": 1537,
            "parallel": 1908,
            "gauge": 2107,
            "funnel": 1692,
            "sankey": 1568
        },
        "components": {
            "geo": 2788,
            "title": 9575,
            "legend": 9400,
            "tooltip": 9466,
            "grid": 9266,
            "markPoint": 3419,
            "markLine": 2984,
            "timeline": 2739,
            "dataZoom": 2744,
            "visualMap": 2466,
            "toolbox": 3034,
            "polar": 1945
        },
        "ie": 9743
    };

    var downloadJson = {
        "echarts.min.js": 17365,
        "echarts.simple.min.js": 4079,
        "echarts.common.min.js": 6929,
        "echarts.js": 14890
    }

    var themeJson = {
        "dark.js": 1594,
        "infographic.js": 925,
        "shine.js": 1608,
        "roma.js": 721,
        "macarons.js": 2179,
        "vintage.js": 1982
    }

    example1();

    //------示例1
    function example1() {
        var xAxisData = [],
            bar1Data = [],
            bar2Data = [];
        self.options = {
            legend: {
                data: ['bar1', 'bar2'],
                align: 'left'
            },
            tooltip: {},
            xAxis: {
                data: xAxisData,
                silent: false,
                splitLine: {
                    show: false
                }
            },
            yAxis: {},
            series: [{
                name: 'bar1',
                type: 'bar',
                data: bar1Data,
            }, {
                name: 'bar2',
                type: 'bar',
                data: bar2Data,
            }]
        };
        //--------------------------------------

        $timeout(function () {
            console.log('模拟异步数据--->5000');
            for (var i = 0; i < 100; i++) {
                xAxisData.push('类目' + i);
                bar1Data.push((Math.sin(i / 5) * (i / 5 - 10) + i / 6) * 5);
                bar2Data.push((Math.cos(i / 5) * (i / 5 - 10) + i / 6) * 5);
            }
        }, 3000);
    }




    //------示例2
    function example2() {
        var data1 = [],
            data2 = [],
            data3 = [],
            data4 = [];
        var vm = $scope.vm = {};

        vm.options = {
            title: [{
                text: '成功率',
                subtext: '总计 ' + builderJson.all,
                x: '25%',
                textAlign: 'center'
            }, {
                text: '波动',
                subtext: '总计 ' + Object.keys(downloadJson).reduce(function (all, key) {
                    return all + downloadJson[key];
                }, 0),
                x: '75%',
                textAlign: 'center'
            }, {
                text: '错误返回占比',
                subtext: '总计 ' + Object.keys(themeJson).reduce(function (all, key) {
                    return all + themeJson[key];
                }, 0),
                x: '75%',
                y: '50%',
                textAlign: 'center'
            }],
            grid: [{
                top: 50,
                width: '50%',
                bottom: '45%',
                left: 10,
                containLabel: true
            }, {
                top: '55%',
                width: '50%',
                bottom: 0,
                left: 10,
                containLabel: true
            }],
            xAxis: [{
                type: 'value',
                max: builderJson.all,
                splitLine: {
                    show: false
                }
            }, {
                type: 'value',
                max: builderJson.all,
                gridIndex: 1,
                splitLine: {
                    show: false
                }
            }],
            yAxis: [{
                type: 'category',
                data: Object.keys(builderJson.charts),
                axisLabel: {
                    interval: 0,
                    rotate: 30
                },
                splitLine: {
                    show: false
                }
            }, {
                gridIndex: 1,
                type: 'category',
                data: Object.keys(builderJson.components),
                axisLabel: {
                    interval: 0,
                    rotate: 30
                },
                splitLine: {
                    show: false
                }
            }],
            series: [{
                type: 'bar',
                stack: 'chart',
                z: 3,
                label: {
                    normal: {
                        position: 'right',
                        show: true
                    }
                },
                data: data1
            }, {
                type: 'bar',
                stack: 'component',
                xAxisIndex: 1,
                yAxisIndex: 1,
                z: 3,
                label: {
                    normal: {
                        position: 'right',
                        show: true
                    }
                },
                data: data2
            }, {
                type: 'pie',
                radius: [0, '30%'],
                center: ['75%', '25%'],
                data: data3
            }, {
                type: 'pie',
                radius: [0, '30%'],
                center: ['75%', '75%'],
                data: data4
            }]
        }


        $timeout(function () {
            console.log('模拟异步数据--->3000');
            $scope.$apply(function () {
                Object.keys(builderJson.charts).map(function (key) {
                    data1.push(builderJson.charts[key]);
                });

                data2 = Object.keys(builderJson.components).map(function (key) {
                    return builderJson.components[key];
                });

                data3 = Object.keys(downloadJson).map(function (key) {
                    return {
                        name: key.replace('.js', ''),
                        value: downloadJson[key]
                    }
                });

                data4 = Object.keys(themeJson).map(function (key) {
                    return {
                        name: key.replace('.js', ''),
                        value: themeJson[key]
                    }
                });
            });


            //$scope.$digest();


        }, 3000);
    }

}

function buildArray(name, size) {
  var i, array = [];
  for (i = 1; i <= size; i++){
    array.push({
      text: name + ' ' + i ,
      value: i
    });
  }

  return array;
};

function sortableController($scope) {
    $scope.list = buildArray('Item', 5);

    $scope.sortingLog = [];

    $scope.sortableOptions = {
        // called after a node is dropped
        stop: function (e, ui) {
            var logEntry = {
                ID: $scope.sortingLog.length + 1,
                Text: 'Moved element: ' + ui.item.scope().item.text
            };
            $scope.sortingLog.push(logEntry);
        }
    }
}

function connectedController($scope) {
    $scope.leftArray = buildArray('Left', 5);
    $scope.rightArray = buildArray('Right', 7);
    $scope.sortableOptions = {
        connectWith: '.connectedItemsExample .list'
    };
}